#define _CRT_SECURE_NO_WARNINGS 1
#include"ListNode.h"
SLN* SLNBuyNode(SLNDataType x)
{
	SLN* node = (SLN*)malloc(sizeof(SLN));
	if (node == NULL)
	{
		perror("malloc failed!");
		exit(1);
	}
	node->data = x;
	node->next = node->prev = node;
	return node;
}
void SLNInit(SLN** pphead)
{
	*pphead = SLNBuyNode(-1);
}
void SLNPrint(SLN* phead)
{
	assert(phead);
	SLN* pcur = phead->next;
	while (pcur != phead)
	{
		printf("%d->", pcur->data);
		pcur = pcur->next;
	}
	printf("\n");
}
bool SLNEmpty(SLN* phead)
{
	assert(phead);
	return phead->next == phead;
}
SLN* SLNFind(SLN* phead,SLNDataType x)
{
	assert(phead);
	SLN* pcur = phead->next;
	while (pcur != phead)
	{
		if (pcur->data == x)
		{
			return pcur;
		}
		pcur = pcur->next;
	}
	return NULL;
}
void SLNPushBack(SLN* phead, SLNDataType x)
{
	assert(phead);
	SLN* newnode = SLNBuyNode(x);
	newnode->next = phead;
	newnode->prev = phead->prev;
	phead->prev->next = newnode;
	phead->prev = newnode;
}
void SLNPushFront(SLN* phead, SLNDataType x)
{
	assert(phead);
	SLN* newnode = SLNBuyNode(x);
	newnode->next = phead->next;
	newnode->prev = phead;
	phead->next->prev = newnode;
	phead->next = newnode;
}
void SLNPopBack(SLN* phead)
{
	assert(phead);
	assert(!SLNEmpty(phead));
	SLN* del = phead->prev;
	SLN* prev = del->prev;
	prev->next = phead;
	phead->prev = prev;
	free(del);
	del = NULL;
}
void SLNPopFront(SLN* phead)
{
	assert(phead);
	assert(!SLNEmpty(phead));
	SLN* del = phead->next;
	SLN* next = del->next;
	phead->next = next;
	next->prev = phead;
	free(del);
	del = NULL;
}
void SLNInsert(SLN* pos, SLNDataType x)
{
	assert(pos);
	SLN* newnode = SLNBuyNode(x);
	newnode->next = pos->next;
	newnode->prev = pos;
	pos->next->prev = newnode;
	pos->next = newnode;
}
void SLNErase(SLN* pos)
{
	assert(pos);
	//pos->prev  pos  pos->next
	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;
	free(pos);
	pos = NULL;
}
void SLNDestroy(SLN** pphead)
{
	assert(pphead && *pphead);
	SLN* pcur = (*pphead)->next;
	while (pcur!=(*pphead))
	{
		SLN* Next = pcur->next;
		free(pcur);
		pcur = Next;
	}
	free(*pphead);
	*pphead = NULL;
	pcur = NULL;
}